package com.example;

import javafx.fxml.FXML;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TextField;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.beans.property.SimpleStringProperty;
import javafx.stage.Stage;
import java.sql.SQLException;
import java.util.List;
import javafx.scene.control.ComboBox;
import java.util.HashSet;
import java.util.Set;

public class TeacherViewClassStudentsController implements TeacherSubController {

    @FXML
    private ComboBox<String> classComboBox;
    @FXML
    private TableView<User> studentTable;
    @FXML
    private TableColumn<User, String> studentIdColumn;
    @FXML
    private TableColumn<User, String> studentNameColumn;
    @FXML
    private TableColumn<User, String> studentClassColumn;

    private Stage primaryStage;
    private TeacherController teacherController;
    private User currentUser;
    private UserDAO userDAO = new UserDAO();

    @FXML
    public void initialize() {
        // Set up the table columns
        studentIdColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getUsername()));
        studentNameColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getName()));
        studentClassColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getDepartment()));
        
        // Load classes into the ComboBox
        loadClassesIntoComboBox();
    }

    private void loadClassesIntoComboBox() {
        try {
            List<User> allUsers = userDAO.getAllUsers();
            Set<String> classes = new HashSet<>();
            for (User user : allUsers) {
                if ("student".equals(user.getType())) {
                    String department = user.getDepartment();
                    if (department != null && !department.trim().isEmpty()) {
                        classes.add(department.trim());
                    }
                }
            }
            ObservableList<String> classList = FXCollections.observableArrayList(classes);
            classComboBox.setItems(classList);

        } catch (SQLException e) {
            e.printStackTrace();
            // Optionally show an error message
        }
    }

    @FXML
    private void handleSearchStudents() {
        String selectedClass = classComboBox.getValue(); // Get selected class from ComboBox

        if (selectedClass == null || selectedClass.trim().isEmpty()) {
            // Optionally show a warning or load all students if no class is selected
            studentTable.setItems(FXCollections.observableArrayList());
            return;
        }

        try {
            // Use searchUsers to filter by the selected department (class name) and user type 'student'
            List<User> filteredStudents = userDAO.searchUsers(null, "student"); // Get all students first
            
            // Manually filter by department/class name
            ObservableList<User> studentsInClass = FXCollections.observableArrayList();
             for (User student : filteredStudents) {
                if (student.getDepartment() != null && student.getDepartment().equalsIgnoreCase(selectedClass.trim())) {
                    studentsInClass.add(student);
                }
            }
            studentTable.setItems(studentsInClass);

        } catch (SQLException e) {
            e.printStackTrace();
            // Optionally show an error message to the user
        }
    }

    @Override
    public void setPrimaryStage(Stage primaryStage) {
        this.primaryStage = primaryStage;
    }

    @Override
    public void setTeacherController(TeacherController teacherController) {
        this.teacherController = teacherController;
    }

    @Override
    public void setCurrentUser(User user) {
        this.currentUser = user;
        // Use currentUser if needed
    }
} 